package com.zhengjiang.mail.biz.service;

import com.zhengjiang.mail.biz.configure.MailProperties;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName MailServiceImpl
 * @Description
 * @Author zhengjiang(99514925 @ qq.com)
 * @Date 2020-05-29 16:39
 * @Version V1.0
 **/
@Service
public class MailServiceImpl implements IMailService {

    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    MailProperties mailProperties;
    private MimeMessageHelper messageHelper;
    private MimeMessage mimeMessage;

    /**
     * 功能描述: 发送简单文本内容邮件
     * @Param :
     * @Return:
     * @Date: 2020-06-01 10:25
    */
    @Override
    public void sendSimpleMail(String subject, String text) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(mailProperties.getFrom());
        simpleMailMessage.setTo(mailProperties.getTo());
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);
        javaMailSender.send(simpleMailMessage);
    }

    /**
     * 功能描述: 发送html格式内容的邮件
     * @Param :
     * @Return:
     * @Date: 2020-06-01 10:26
    */
    @Override
    public void sendHtmlMail(String subject, String text, Map<String, String> attachmentMap) throws MessagingException {
        buildMimeMessageHelper();
        messageHelper.setSubject(subject);
        messageHelper.setText(text,true); //重点，默认为false，显示原始html代码，无效果
        if (attachmentMap != null) {
            attachmentMap.entrySet().stream().forEach(entrySet -> {
                File file = new File(entrySet.getValue());
                if (file.exists()) {
                    try {
                        messageHelper.addAttachment(entrySet.getKey(), new FileSystemResource(file));
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        javaMailSender.send(mimeMessage);
    }

    /**
     * 功能描述: 发送邮件模板内容邮件
     * @Param :
     * @Return:
     * @Date: 2020-06-01 10:26
    */
    @Override
    public void sendTemplateMail(String subject, Map<String, Object> params) throws MessagingException, IOException, TemplateException {
        buildMimeMessageHelper();
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        configuration.setClassForTemplateLoading(this.getClass(), "/ftl");
        String html = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("mail.ftl"), params);
        messageHelper.setSubject(subject);
        messageHelper.setText(html, true);//重点，默认为false，显示原始html代码，无效果
        javaMailSender.send(mimeMessage);
    }

    private void buildMimeMessageHelper() throws MessagingException {
        mimeMessage = javaMailSender.createMimeMessage();
        messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(mailProperties.getFrom());
        messageHelper.setTo(mailProperties.getTo());
    }
}
